from django.shortcuts import render
from django.http.response import JsonResponse
from rest_framework.parsers import JSONParser
from rest_framework.decorators import api_view
from student_train.utils import set_token, get_token, is_blank
from login.models import Student, Subject, Car_info, Student_train, Student_teach


# Create your views here.

# 学员预约训练表
# class Student_train(models.Model):
#     train_id = models.IntegerField(auto_created=True, primary_key=True, verbose_name='预约id')
#     student = models.ForeignKey(Student, on_delete=models.SET_NULL, null=True, verbose_name='关联学员信息')
#     test = models.ForeignKey(Subject, on_delete=models.SET_NULL, null=True, verbose_name='关联考试科目')
#     car = models.ForeignKey(Car_info, on_delete=models.SET_NULL, null=True, verbose_name='关联车辆信息')
#     ground_id = models.CharField(max_length=100, verbose_name='预约场地')
#     session = models.CharField(max_length=100, verbose_name='预约时间段')
#     is_status = models.BooleanField(null=False, default=False, verbose_name='使用预约')
@api_view(["get"])
def train_list(request):
    token = request.headers.get('token')
    is_status = request.GET.get("is_status")
    session = request.GET.get("session")
    if not token:
        JsonResponse({"code": 400, "message": "请登录"})
    jiema = get_token(token)
    data = {}
    if not is_blank(is_status):
        data["is_status"] = is_status
    if not is_blank(session):
        data["session"] = session
    student_id = jiema['id']
    data["student__id"] = student_id
    queryset = Student_train.objects.filter(**data).values()
    total = queryset.count()
    list = []
    for e in queryset:
        list.append({
            "train_id": e.get("train_id"),
            "student": Student.objects.filter(id=student_id).values().first(),
            "test": Subject.objects.filter(subject_id=e["test_id"]).values().first(),
            "car": Car_info.objects.filter(car_id=e["car_id"]).values().first(),
            "ground_id": e.get("ground_id"),
            "session": e.get("session"),
            "is_status": e.get("is_status"),
        })
    return JsonResponse({"code": 200, "message": "查询成功", "data": list, "total": total})


@api_view(["post"])
def train_add(request):
    token = request.headers.get('Token')
    if not token:
        JsonResponse({"code": 400, "message": "请登录"})
    jiema = get_token(token)

    student_id = jiema['id']
    print(student_id)

    data = JSONParser().parse(request)
    data['student_id'] = student_id
    Student_train(**data).save()
    return JsonResponse({"code": 200, "message": "添加成功", "data": data})


@api_view(["put"])
def train_change(request, train_id):
    token = request.headers.get('token')
    if not token:
        JsonResponse({"code": 400, "message": "请登录"})
    data = JSONParser().parse(request)
    Student_train.objects.filter(train_id=train_id).update(**data)
    return JsonResponse({"code": 200, "message": "修改成功"})


@api_view(["post"])
def train_out(request, train_id):
    token = request.headers.get('token')
    if not token:
        JsonResponse({"code": 400, "message": "请登录"})
    data = JSONParser().parse(request)
    train = Student_train.objects.filter(train_id=train_id)
    train.update(**data)
    item = train.values().first()
    car_id = item["car_id"]
    coa_id = Car_info.objects.filter(car_id=car_id).values().first()["coa_id"]
    teach = Student_teach(stu_id=item["student_id"], car_id=car_id, ground=item["ground_id"],
                          test_id=item["test_id"], coa_id=coa_id, record_id=train_id, train_id=train_id)
    teach.save()
    return JsonResponse({"code": 200, "message": "修改成功", "data": item})
